package leetcode.top100;

/**
 * @date 2020/4/5 21:02
 */
public class Code75_NetherFlag {
    public void sortColors(int[] nums) {
        if(nums == null || nums.length <= 1) return;
        int target = 1;
        int less = -1;
        int more = nums.length;
        int p = 0;
        while (p < more){
            if(nums[p] < target){
                swap(nums,++less,p++);
            }else if(nums[p] > target){
                swap(nums,p,--more);
            }else {
                p++;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        if(i == j )return;
        nums[i] = nums[i] ^ nums[j];
        nums[j] = nums[i] ^ nums[j];
        nums[i] = nums[i] ^ nums[j];
    }
}
